package com.lichongbing.lyoggl.controller;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.lichongbing.lyoggl.model.Money;
import com.lichongbing.lyoggl.model.Project;
import com.lichongbing.lyoggl.service.MoneyService;
import com.lichongbing.lyoggl.utils.JwtTokenUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.UUID;

@Slf4j
@RestController
@RequestMapping("/money")
/**
 * @author lichongbing
 * @version 1.0.0
 * @createdate 2022/8/10 15:17
 * @description: TODO
 */
public class MoneyController {
    @Autowired
    private MoneyService moneyService;

    /**
     * 新增明细
     *
     * @param jsonObject
     * @param request
     * @return
     */
    @PostMapping("/create")
    public String createProject(@RequestBody JSONObject jsonObject, HttpServletRequest request) {
        String token = request.getHeader("token");
        String dateTime = DateUtil.format(new Date(), "YYYY年MM月dd日HH时mm分");
        boolean expiration = JwtTokenUtils.isExpiration(token);
        String year = String.valueOf(DateUtil.thisYear());
        String mouth = String.valueOf(DateUtil.thisMonth() + 1);
        int day = DateUtil.thisDayOfMonth();
        Money project = new Money();
        String id = UUID.randomUUID().toString().replace("-", "");
        if (expiration == false) {
            String userid = JwtTokenUtils.getUserid(token);
            project.setId(id);
            project.setDateTime(dateTime);
            project.setMoney(jsonObject.getString("money"));
            project.setType(Integer.valueOf(jsonObject.getString("type")));
            project.setWays(jsonObject.getString("ways"));
            project.setYear(year);
            project.setMouth(mouth);
            project.setUid(userid);
            project.setDay(String.valueOf(day));
            moneyService.save(project);
            return "添加成功";
        } else {
            return "token过期";
        }
    }

    /**
     * 根据用户id查询明细
     *
     * @param request
     * @return
     */
    @PostMapping("/findByUid")
    public JSONObject findByUid(HttpServletRequest request) {
        JSONObject jsonObject = new JSONObject();
        String token = request.getHeader("token");
        boolean expiration = JwtTokenUtils.isExpiration(token);
        if (expiration == false) {
            String userid = JwtTokenUtils.getUserid(token);
            jsonObject.put("code", 200);
            jsonObject.put("msg", "查询成功");
            jsonObject.put("data", moneyService.findByUid(userid));
            return jsonObject;
        } else {
            jsonObject.put("code", 400);
            jsonObject.put("msg", "token过期");
            return jsonObject;
        }
    }


    /**
     * 根据id删除明细
     *
     * @param id
     * @param request
     * @return
     */
    @PostMapping("/delete/{id}")
    public JSONObject delete(@PathVariable String id, HttpServletRequest request) {
        JSONObject jsonObject = new JSONObject();
        String token = request.getHeader("token");
        boolean expiration = JwtTokenUtils.isExpiration(token);
        if (expiration == false) {
            String userid = JwtTokenUtils.getUserid(token);
            moneyService.deleteByUid(id, userid);
            jsonObject.put("code", 200);
            jsonObject.put("msg", "删除成功");
            return jsonObject;
        } else {
            jsonObject.put("code", 400);
            jsonObject.put("msg", "token过期");
            return jsonObject;
        }
    }

    @PostMapping("/update")
    public JSONObject update(@RequestBody JSONObject money, HttpServletRequest request) {
        JSONObject jsonObject = new JSONObject();
        try {

            String token = request.getHeader("token");
            boolean expiration = JwtTokenUtils.isExpiration(token);
            if (expiration == false) {
                this.updateService(money);
                jsonObject.put("code", 200);
                jsonObject.put("msg", "更新成功");
                return jsonObject;
            } else {
                jsonObject.put("code", 400);
                jsonObject.put("msg", "token过期");
                return jsonObject;
            }

        } catch (Exception e) {
            jsonObject.put("code", 500);
            jsonObject.put("msg", "系统错误");
            return jsonObject;
        }


    }

    /**
     * 更新操作
     *
     * @param moneys
     */

    private boolean updateService(JSONObject moneys) {
        JSONObject res = new JSONObject();
        String id = moneys.getString("id");
        String dateTime = DateUtil.format(new Date(), "YYYY年MM月dd日HH时mm分");
        String money = moneys.getString("money");
        String type = moneys.getString("type");
        String ways = moneys.getString("ways");
        String mouth = moneys.getString("mouth");
        String year = moneys.getString("year");
        UpdateWrapper<Money> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", id).set("date_time", dateTime);
        if (!"".equals(money)) {
            updateWrapper.set("money", money);
        }
        if (!"".equals(type)) {
            updateWrapper.set("type", type);
        }
        if (!"".equals(ways)) {
            updateWrapper.set("ways", ways);
        }
        if (!"".equals(mouth)) {
            updateWrapper.set("mouth", mouth);
        }
        if (!"".equals(year)) {
            updateWrapper.set("year", year);
        }

        boolean update = moneyService.update(null, updateWrapper);
        return update;
    }


}
